import re

from flask import Flask, session, render_template, redirect, Blueprint, request

from config import Config
from db import db
from views.page import page
from views.user import user


app = Flask(__name__)
# 初始化配置
app.config.from_object(Config)
# 配置数据库
db.init_app(app)

# 注册蓝图对象
app.register_blueprint(page.page_bp)
app.register_blueprint(user.user_bp)


# @app.route('/helloworld')
# def hello_world():
#     return 'Hello World!'


@app.route('/')
def index():
    return redirect('/user/login')


@app.before_request
def before_request():
    pat = re.compile(r'^/static')
    if re.search(pat, request.path):
        return None
    if request.path == '/user/login':
        return None
    if request.path == '/user/register':
        return None
    username = session.get('username')
    if username:
        return None
    return redirect('/user/login')


@app.route('/<path:path>')
def catch_all(path):
    return render_template('404.html')


if __name__ == '__main__':
    app.run()
